#include <arpa/inet.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>


int main() {
  // 1. 创建客户端，并连接到服务端
  int sock_client = socket(AF_INET, SOCK_STREAM, 0);
  sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(8888);
  connect(sock_client, (sockaddr*)&server_addr, sizeof(sockaddr));

  // 2. 发送数据，并接受服务端数据
  char* send_info = {"How are you?"};
  while(1) {
    send(sock_client, send_info, strlen(send_info) + 1, 0);
    printf("send:    %s\n", send_info);

    char recv_info[50];
    recv(sock_client, recv_info, sizeof(recv_info), 0);
    printf("receive: %s\n", recv_info);
  }

  // 3. 关闭客户端
  close(sock_client);
  return 0;
}
